'''
Created on Sep 29, 2012

@author: johnny
'''
import unittest, random, time
from db import databases
import vserver, cmd


class Test(unittest.TestCase):


    def setUp(self):
        self.vserver = vserver.create_vserver("192.168.200.6")
        self.control = cmd.Manager()
        self.query = databases.DBQuery("192.168.200.6")


    def tearDown(self):
        pass


    def testName(self):
        snaps = self.query.get_snapshots('xp')
        my_vm = self.vserver.get_exist_vm('xp')
        my_vm.start(True)
        my_vm.is_started()
#        snap = random.choice(snaps)
#        if not query.is_current_snapshot(snap):
#            self.control.recover_snapshot(my_vm, snap)
        while True:
            snaps = self.query.get_snapshots('xp')
            snap = random.choice(snaps)
            if not self.query.is_current_snapshot(snap):
                self.control.recover_snapshot(my_vm, snap)
                my_vm.start_vnc()
                my_vm.is_started(20)
                time.sleep(10)

#    def testSnap(self):
#        my_vm = self.vserver.get_exist_vm('xp-1')
#        my_vm.start(True)
#        my_vm.is_started()
#        
#        self.control.snapshot(my_vm, "1")
#        time.sleep(30)


if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.testName']
    unittest.main()